package com.example.share_doc.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.share_doc.controller.dto.DepartmentDto;
import com.example.share_doc.controller.dto.DepartmentQueryDto;
import com.example.share_doc.entity.Department;
import com.example.share_doc.entity.User;
import com.example.share_doc.mapper.DepartmentMapper;
import com.example.share_doc.mapper.UserMapper;
import com.example.share_doc.util.Common;
import com.example.share_doc.util.MinioUtil;
import io.minio.Result;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@AllArgsConstructor
@Slf4j
public class DepartmentService {

    private DepartmentMapper departmentMapper;

    private UserMapper userMapper;

    private MinioUtil minioUtil;

    public List<DepartmentDto> queryList(DepartmentQueryDto departmentQueryDto) {
        // 1. 初始化
        List<DepartmentDto> departmentTree = new ArrayList<>();
        // 2. 查询部门数据
        QueryWrapper<Department> departmentQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(departmentQueryDto.getDepartmentName())) {
            departmentQueryWrapper.lambda().like(Department::getDepartmentName, departmentQueryDto.getDepartmentName());
        }
        if (StringUtils.isNotBlank(departmentQueryDto.getDepartmentCode())) {
            departmentQueryWrapper.lambda().eq(Department::getDepartmentCode, departmentQueryDto.getDepartmentCode());
        }
        List<Department> departmentList = departmentMapper.selectList(departmentQueryWrapper);
        // 3. 组合数据
        HashMap<String, List<Department>> departmentMap = new HashMap<>();
        departmentList.forEach(department -> {
            String departmentParentCode = department.getDepartmentParentCode();
            departmentMap.put(departmentParentCode, new ArrayList<>());
        });
        departmentList.forEach(department -> {
            String departmentParentCode = department.getDepartmentParentCode();
            if (StringUtils.isNotBlank(departmentParentCode)){
                departmentMap.get(departmentParentCode).add(department);
            }
        });
        List<Department> departmentRoot = departmentMap.get(Common.DEPARTMENT_ROOT_CODE);
        departmentRoot.forEach(department -> {
            findChildrenDepartment(department, departmentMap);
        });
        // 4. 数据转换 TODO
        departmentRoot.forEach(department -> {
            String bucketName = department.getDepartmentCode();
            Iterable<Result<Item>> object = minioUtil.listObjects(bucketName);

        });
        // 4. 返回
        return departmentTree;
    }

    private void findChildrenDepartment(Department department, HashMap<String, List<Department>> departmentMap) {
        List<Department> children = departmentMap.get(department.getDepartmentCode());
        if (children != null){
            department.setChildren(children);
            for (Department child : children){
                findChildrenDepartment(child, departmentMap);
            }
        }
    }

    public List<User> queryUserList(String departmentCode) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getDepartmentCode, departmentCode);
        return userMapper.selectList(queryWrapper);
    }

    public void departmentAddOrUpdate(Department department) {
        departmentMapper.insertOrUpdate(department);
        minioUtil.createBucket(department.getDepartmentCode());
    }

    public void departmentDeleteList(List<String> departmentIdList) {
        List<Department> departmentList = departmentMapper.selectBatchIds(departmentIdList);
        departmentList.forEach(department -> {
            String departmentCode = department.getDepartmentCode();
            // 判断是否存在子部门
            QueryWrapper<Department> departmentQueryWrapper = new QueryWrapper<>();
            departmentQueryWrapper.lambda().eq(Department::getDepartmentParentCode, departmentCode);
            Long childrenCount = departmentMapper.selectCount(departmentQueryWrapper);
            Assert.isTrue(childrenCount == 0L, "该部门下存在子部门，删除失败");
            // 判断是否存在用户
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.lambda().eq(User::getDepartmentCode, departmentCode);
            Long userCount = userMapper.selectCount(userQueryWrapper);
            Assert.isTrue(userCount == 0L, "该部门下存在用户，删除失败");
            // 删除桶
            minioUtil.removeBucket(department.getDepartmentCode());
        });
        departmentMapper.deleteByIds(departmentIdList);
    }
}
